// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Title Card Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 25 : TYPE_TITLECARD

event ObjectMain
	options.touchControls = 0
	switch object.state
	case 0
		// Init values and such
		object.alpha = 255
		object.value1 = screen.xcenter
		object.value1 += 128
		object.value2 = screen.xcenter
		FlipSign(object.value2)
		object.value2 -= 160
		object.value3 = screen.xcenter
		FlipSign(object.value3)
		object.value3 -= 96
		object.value4 = screen.xcenter
		object.value4 += 32
		if credits.screen > 0
			object.state = 7
			object.value0 = 272
			stage.state = STAGE_RUNNING
			SetScreenFade(0, 0, 0, 255)
		else
			object.state++
		end if
		break

	case 1
		// Wait for half a second
		if object.value0 < 30
			object.value0++
		else
			object.value0 = 0
			PlayMusic(0)
			object.state++
		end if
		break

	case 2
		// Slide stuff in
		if object.value1 > 0
			object.value1 -= 16
			if object.value1 < 0
				object.value1 = 0
			end if
		end if

		if object.value2 < 0
			object.value2 += 16
			if object.value2 > 0
				object.value2 = 0
			end if
		else
			if object.value3 < 0
				object.value3 += 16
				if object.value3 > 0
					object.value3 = 0
				end if
			else
				if object.value4 > 0
					object.value4 -= 16
					if object.value4 < 0
						object.value4 = 0
					end if
				else
					object.state++
				end if
			end if
		end if
		break

	case 3
		if object.value0 < 60
			object.value0++
		else
			object.value0 = 0
			object.alpha = 248
			stage.state = STAGE_RUNNING
			currentPlayer = 0
			while currentPlayer < playerCount
				object[currentPlayer].controlMode = -1
				object[currentPlayer].up = 0
				object[currentPlayer].down = 0
				object[currentPlayer].left = 0
				object[currentPlayer].right = 0
				object[currentPlayer].jumpPress = 0
				object[currentPlayer].jumpHold = 0
				object[currentPlayer].value1 = 0
				currentPlayer++
			loop
			object.state++
		end if
		break

	case 4
		if object.alpha > 0
			object.alpha -= 8
		else
			object.state++
			currentPlayer = 0
			while currentPlayer < playerCount
				object[currentPlayer].controlMode = 0
				currentPlayer++
			loop
		end if
		break

	case 5
		if object.value0 < 60
			object.value0++
		else
			object.value0 = 0
			stage.timeEnabled = 1
			object.state++
		end if
		break

	case 6
		object.value1 += 32
		object.value4 += 32
		object.value2 -= 32
		object.value3 -= 32
		if object.value0 < 20
			object.value0++
		else
			object.value0 = 0
			object.type = TypeName[Blank Object]
		end if
		break

	case 7
		if object.value0 > 0
			object.value0 -= 16
			SetScreenFade(0, 0, 0, object.value0)
		else
			currentPlayer = 0
			while currentPlayer < playerCount
				object[currentPlayer].controlMode = 0
				currentPlayer++
			loop
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break

	case 8
		if object.value0 < 300
			object.value0 += 5
			if credits.screen == 0
				music.volume -= 2
			end if
		else
			stage.activeList = PRESENTATION_STAGE
			if credits.screen > 0
				stage.listPos = 2
			else
				stage.listPos = 0
				StopMusic()
			end if
			LoadStage()
		end if
		SetScreenFade(0, 0, 0, object.value0)
		break

	end switch
end event


event ObjectDraw
	if object.state < 7
		SetActivePalette(0, 0, screen.ysize)
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, object.alpha)
		temp0 = screen.xcenter
		temp0 -= 48
		temp0 += object.value3
		temp0 += object.propertyValue
		DrawSpriteScreenXY(26, temp0, 86)
		temp0 = screen.xcenter
		temp0 += 22
		temp0 += object.value2
		temp0 += object.propertyValue
		DrawActName(0, temp0, 80, 0, 0, 16, 1)
		temp0 = screen.xcenter
		temp0 -= 57
		temp0 += object.value3
		temp0 += object.propertyValue
		DrawSpriteScreenXY(27, temp0, 100)
		temp0 = screen.xcenter
		temp0 -= 21
		temp0 += object.value4
		temp0 += object.propertyValue

		if stage.actNum < 5
			temp1 = 27
			temp1 += stage.actNum
			if temp1 > 30
				temp1 = 30
			end if
			DrawSpriteScreenXY(temp1, temp0, 102)
		end if
	end if
end event


event ObjectStartup

	LoadSpriteSheet("Global/TitleCard.gif")
	SpriteFrame(20, 0, 17, 24, 11, 179) /A
	SpriteFrame(20, 0, 16, 24, 30, 179) /B
	SpriteFrame(20, 0, 15, 24, 47, 179) /C
	SpriteFrame(20, 0, 15, 24, 65, 179) /D
	SpriteFrame(20, 0, 17, 24, 82, 179) /E
	SpriteFrame(20, 0, 16, 24, 101, 179) /F
	SpriteFrame(20, 0, 16, 24, 119, 179) /G
	SpriteFrame(20, 0, 16, 24, 137, 179) /H
	SpriteFrame(20, 0, 9, 24, 155, 179) /I
	SpriteFrame(20, 0, 9, 24, 166, 179) /J
	SpriteFrame(20, 0, 16, 24, 177, 179) /K
	SpriteFrame(20, 0, 8, 24, 195, 179) /L
	SpriteFrame(20, 0, 24, 24, 205, 179) /M
	SpriteFrame(20, 0, 15, 24, 231, 179) /N
	SpriteFrame(20, 0, 24, 24, 11, 205) /O
	SpriteFrame(20, 0, 16, 24, 37, 205) /P
	SpriteFrame(20, 0, 24, 24, 55, 205) /Q
	SpriteFrame(20, 0, 16, 24, 81, 205) /R
	SpriteFrame(20, 0, 16, 24, 99, 205) /S
	SpriteFrame(20, 0, 16, 24, 117, 205) /T
	SpriteFrame(20, 0, 16, 24, 135, 205) /U
	SpriteFrame(20, 0, 15, 24, 153, 205) /V
	SpriteFrame(20, 0, 24, 24, 170, 205) /W
	SpriteFrame(20, 0, 16, 24, 195, 205) /X
	SpriteFrame(20, 0, 15, 24, 213, 205) /Y
	SpriteFrame(20, 0, 16, 24, 230, 205) /Z
	SpriteFrame(-150, -130, 64, 225, 345, 0) /Ball
	if stage.actNum > 4
		SpriteFrame(28, 10, 72, 24, 79, 233) /Zone
	else
		SpriteFrame(28, 10, 72, 24, 79, 233) /Zone
	end if
	SpriteFrame(10, 40, 49, 32, 11, 230) /Act 1
	SpriteFrame(10, 40, 49, 32, 11, 266) /Act 2
	SpriteFrame(10, 40, 48, 32, 11, 300) /Act 3

	// Cycle through all objects and filter out title cards
	// (Pretend this is a foreach)
	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Title Card]
			object[arrayPos0].priority = PRIORITY_ACTIVE_PAUSED
			object[arrayPos0].alpha = 255
			object[arrayPos0].drawOrder = 6
			stage.state = STAGE_PAUSED
		end if
		arrayPos0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
